﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Common
{
    public class PagerForShenSuan
    {
        #region 属性
        private string css = "rightpart2";

        /// <summary>
        /// 分页容器DIV样式(默认:the_pages)
        /// </summary>
        public string PageDivCSS
        {
            get { return css; }
            set { css = value; }
        }

        private int totalEntriesCount;
        /// <summary>
        /// 总条目数目
        /// </summary>
        public int RecordCount
        {
            get
            {
                return this.totalEntriesCount;
            }
            set { this.totalEntriesCount = value; }
        }
        public string RecordCss
        {
            get;
            set;
        }
        private int _countPerPage = 1;
        /// <summary>
        /// 每页条目数目
        /// </summary>
        public int PageSize
        {
            get
            {
                return this._countPerPage;
            }
            set
            {
                this._countPerPage = value;
            }
        }
        private int _pageNo;
        /// <summary>
        /// 页码
        /// </summary>
        public int CurrentPageIndex
        {
            get
            {
                if (_pageNo == 0)
                {
                    if (HttpContext.Current.Request.QueryString["page"] != null)
                    {
                        int.TryParse(HttpContext.Current.Request.QueryString["page"].ToString(), out this._pageNo);
                    }
                    if (this._pageNo == 0) this._pageNo = 1;
                }
                return _pageNo > PageCount ? PageCount : _pageNo;
            }
            set
            {
                _pageNo = value;
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                if (RecordCount == 0)
                    return 1;
                return (int)Math.Ceiling((double)RecordCount / (double)PageSize);
            }
        }

        #endregion

        public string RenderQueryString(StringBuilder writer, string cssForLast,string cssForNext)
        {
            if (this.PageCount > 1)
            {
                this.RenderLastPageBlock(writer, cssForLast);
                this.RenderNextPageBlock(writer, cssForNext);
            }
            return writer.ToString();
        }

        private void RenderLastPageBlock(StringBuilder writer, string cssForLast)
        {
            if (this.CurrentPageIndex > 1)
            {
                writer.Append(string.Format("<a class=\"{1}\" href=\"javascript:void(0);\" onclick=\"changePage('{0}')\"></a>", (this.CurrentPageIndex - 1),cssForLast));
            }
            else
            {
                writer.Append("<a href='javascript:void(0)' class=\"nofanye\"></a>");
            }
        }
        private void RenderNextPageBlock(StringBuilder writer, string cssForNext)
        {
            if (this.CurrentPageIndex < this.PageCount)
            {
                writer.Append(string.Format("<a class=\"{1}\" href=\"javascript:void(0);\" onclick=\"changePage('{0}')\"></a>", (this.CurrentPageIndex + 1), cssForNext));
            }
            else
            {
                writer.Append("<a class=\"nofanye\" href='javascript:void(0)'></a>");
            }
        }
    }
}
